package com.cmw.auntapp.presenter;

import android.text.TextUtils;
import android.util.Log;

import com.alibaba.fastjson.JSONObject;
import com.cmw.auntapp.R;
import com.cmw.auntapp.constant.API;
import com.cmw.auntapp.constant.CommandType;
import com.cmw.auntapp.constant.Constant;
import com.cmw.auntapp.core.base.BasePresenter;
import com.cmw.auntapp.core.base.IBaseView;
import com.cmw.auntapp.core.base.vo.DataResult;
import com.cmw.auntapp.core.util.HttpUtil;
import com.cmw.auntapp.core.util.SharedPreferencesUtil;
import com.cmw.auntapp.core.util.StringUtils;
import com.cmw.auntapp.core.util.ValidUtil;
import com.cmw.auntapp.model.AuntModel;
import com.google.android.gms.common.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import static com.cmw.auntapp.constant.API.AUNT_WORK_STATUS_ON_UN;
import static com.cmw.auntapp.constant.API.SMS_CODE_SEND;
import static com.cmw.auntapp.constant.Constant.SHOW_LOADING_KEY;
import static com.cmw.auntapp.constant.Constant.SHOW_LOADING_YES;
import static com.cmw.auntapp.constant.Constant.USER_INEMPID_KEY;
import static com.cmw.auntapp.constant.Constant.USER_MEMBERID_KEY;
import static com.cmw.auntapp.constant.Constant.USER_RNAME_KEY;
import static com.cmw.auntapp.constant.Constant.USER_USERAUTHTOKEN_KEY;

/**
 * @desc: 阿姨信息 Presenter
 * @author: chengmingwei
 * @date: 2021/05/07 07:36 AM
 * @contact: phone(微信同号):13922111489 / email: 340360491@qq.com
 */
public class AuntPresenter extends BasePresenter<AuntModel> {
    private static final String TAG = "AuntPresenter";

    private IBaseView iBaseView;

    private AuntModel auntModel;

    private LoginPresenter loginPresenter = null;

    public AuntPresenter(IBaseView iBaseView) {
        this.iBaseView = iBaseView;
        init();
        loginPresenter = new LoginPresenter(iBaseView);
    }

    public void get() {
        get(null);
    }

    public void get(Map<String, String> appendPars) {
        Long memberId = SharedPreferencesUtil.getInstance(getContext()).getLong(USER_MEMBERID_KEY);
        Map<String, String> params = new HashMap<>();
        params.put("memberId", memberId.toString());
        params.put("loadInsureInfo", "1");
        if (null != appendPars && appendPars.size() > 0) {
            params.putAll(appendPars);
        }
        get(API.AUNT_INFO_GET_PARS, CommandType.GET, params);
    }
    /**
     * 离岗
     */
    public void unWork(){
        setWorkStatus(Constant.AUNT_PSTATUS_0);
    }

    /**
     * 上岗
     */
    public void onWork(){
        setWorkStatus(Constant.AUNT_PSTATUS_1);
    }

    /**
     * 上/下岗
     * @param pstatus
     */
    public void setWorkStatus(Integer pstatus){
        Map<String, String> params = new HashMap<>();
        params.put("pstatus", pstatus.toString());
        put(API.parseAPI(AUNT_WORK_STATUS_ON_UN, params, "pstatus"), params, CommandType.PUT);
    }

    /**
     * 阿姨信息验证
     *  姓名、手机号、性别、从事钟点工年龄、籍贯、服务类型、家庭住址、身份证号码、保险费、保险到期日
     *      *             et_name,et_phone,rgp_sex,et_age, sp_place, sp_service_type,et_address,et_card_no,et_premium,et_end_date
     *      *             //附件类：
     *      *             身份证正面,身份证反面,保险上传按钮,保险合同1,保险合同2
     *      *             ib_id_card_front,ib_id_card_back,fl_insure_contract,fm_upload_contract2/ib_upload_contract2,fm_upload_contract3/ib_upload_contract3
     * @param auntModel
     * @return
     */
    public Map<Integer, String> valid(AuntModel auntModel) {
        Map<Integer, String> errResult = new HashMap<Integer, String>();
        String name = auntModel.getName();  // 姓名
        String phone = auntModel.getPhone(); // 手机号
        Byte sex = auntModel.getSex();  // 性别
        Integer age = auntModel.getAge(); // 从事钟点工年龄
        Long place = auntModel.getNativePlace(); // 籍贯
        String serviceTypes = auntModel.getServiceTypes(); // 服务类型
        String address = auntModel.getAddress(); // 家庭住址
        String cardNo = auntModel.getIdcardNo(); // 身份证号码
        BigDecimal premium = auntModel.getPremium(); // 保险费
        Date endDate = auntModel.getEndDate(); // 保险到期日
        String attachmentIds = auntModel.getAttachmentIds(); // 身份证正面,身份证反面
        String insureAttIds = auntModel.getInsureAttIds(); // 保险合同1,保险合同2

        if (TextUtils.isEmpty(name)) {
            errResult.put(R.id.et_name, "请输入姓名");
            return errResult;
        }
        if (name.length() < 2) {
            errResult.put(R.id.et_name, "姓名长度必须大于2个字符");
            return errResult;
        }

        if (name.length() > 20) {
            errResult.put(R.id.et_name, "姓名长度不能超过20个字符");
            return errResult;
        }

        if (TextUtils.isEmpty(phone)) {
            errResult.put(R.id.et_phone, "请输入手机号");
            return errResult;
        }

        if (!ValidUtil.isPhoneLegal(phone)) {
            errResult.put(R.id.et_phone, "手机号格式不正确");
            return errResult;
        }

        if (sex == null) {
            errResult.put(R.id.rgp_sex, "请选择性别");
            return errResult;
        }

        if (age == null) {
            errResult.put(R.id.et_age, "请输入从事钟点工的年龄");
            return errResult;
        }

        if (age < 0) {
            errResult.put(R.id.et_age, "年龄不得小于0岁");
            return errResult;
        }

        if (age > 100) {
            errResult.put(R.id.et_age, "年龄不得超过100岁");
            return errResult;
        }

        if (place == null){
            errResult.put(R.id.sp_place, "请选择籍贯");
            return errResult;
        }

        if (TextUtils.isEmpty(serviceTypes)) {
            errResult.put(R.id.sp_service_type, "请选择服务类型");
            return errResult;
        }

        if (TextUtils.isEmpty(address)) {
            errResult.put(R.id.et_address, "请输入家庭住址");
            return errResult;
        }

        if (address.length() < 2) {
            errResult.put(R.id.et_address, "家庭住址长度必须大于2个字符");
            return errResult;
        }

        if (address.length() > 20) {
            errResult.put(R.id.et_address, "家庭住址长度不能超过100个字符");
            return errResult;
        }

        if (TextUtils.isEmpty(cardNo)) {
            errResult.put(R.id.et_card_no, "请输入身份证号码");
            return errResult;
        }
        if (!ValidUtil.isCardNumber(cardNo)) {
            errResult.put(R.id.et_card_no, "身份证号码格式错误");
            return errResult;
        }

        //身份证正面,身份证反面
        if (TextUtils.isEmpty(attachmentIds) || attachmentIds.split(",").length < 2) {
            errResult.put(R.id.tv_pros_cons, "请上传身份证正/反面照片");
            return errResult;
        }

        //保险合同上传
        if (TextUtils.isEmpty(insureAttIds) || insureAttIds.split(",").length < 2) {
            errResult.put(R.id.tv_insure_contract, "请上传两张意外保险合同照片");
            return errResult;
        }

        if (premium == null || premium.doubleValue() <= 0) {
            errResult.put(R.id.et_premium, "保险费不能为空且必须大于0");
            return errResult;
        }
        if (endDate == null) {
            errResult.put(R.id.et_end_date, "请选择保险到期日");
            return errResult;
        }
        return null;
    }

    /**
     * 保存阿姨信息
     * @param auntModel
     */
    public void doSave(AuntModel auntModel) {
        this.auntModel = auntModel;
        postBody(API.AUNT_INFO_SAVE, auntModel, CommandType.SAVE);
    }

    @Override
    protected void success(CommandType commandType, DataResult dataResult) {
        switch (commandType) {
            case GET:{
                this.auntModel = dataResult.getData(AuntModel.class);
                if (this.auntModel == null) {
                    Log.e(TAG, "阿姨详情数据为空,可能是阿姨还未填写实名认证资料！");
                    return;
                }
                updateMemberInfo2Cache(null, null);
                break;
            } case SAVE:{ //保存
                JSONObject jsonObject = (JSONObject)dataResult.getDatas();
                Long inempId = jsonObject.getLong("id");
                String userToken = jsonObject.getString(USER_USERAUTHTOKEN_KEY);
                updateMemberInfo2Cache(inempId, userToken);
                break;
            }
        }
    }

    private void updateMemberInfo2Cache(Long inempId, String userToken) {
        if (null == inempId) inempId = auntModel.getId();
        if (StringUtils.isEmpty(userToken)){
            return;
        }
        Log.i(TAG, "inempId = "+inempId+", userToken = "+userToken);
        loginPresenter.setRealAuthInfo(userToken, auntModel.getName(), inempId);
    }

    @Override
    protected IBaseView getView() {
        return iBaseView;
    }


}
